/**
 * Created by Administrator on 2017/6/10.
 */

var DB =  require('../db/db');
var IncreaseId = require('../tools/IncreaseId');
var MD5 = require('../tools/MD5');

/**登陆收银机*/
exports.cashierLoading = function(req,res,msg) {
    var username = req.body.username;
    var password = req.body.password;
    var token = IncreaseId.increaseId();
    var time = new Date().getTime()+24*60*60*1000;
    if (username && password) {
        var sql = "select * from k_cashier WHERE user_name ='" + username + "' AND password ='"+password+"'";
        DB.query(sql, function (er, list) {
            if (!er && list.length>0) {
                var sql2 = "UPDATE k_cashier SET token ='" + token + "',time ='" + time + "' WHERE user_name ='" + username + "' AND password ='"+password+"'";
                console.log(sql2);
                DB.query(sql2, function (er, list) {
                    if (!er) {
                        msg.success = true;
                        msg.code = 100;
                        msg.data = token;
                        res.json(msg);
                    } else {
                        msg.msg = er;
                        res.json(msg);
                    }
                });
            }else {
                msg.code = 103;
                msg.msg = "用户名或密码错误";
                res.json(msg);
            }
        });
    }else{
        msg.msg = "用户名或密码错误";
        res.json(msg);
    }
};

/**Pose机成功登陆之后验证*/
exports.cashierLoaded= function(req,res,msg){
    var token = req.body.token;
    var username = req.body.username;
    var sql = "SELECT token,time from k_cashier WHERE user_name ='"+ username +"'";
    console.log(sql);
    DB.query(sql, function (er, list) {
        if (!er && list.length > 0) {
            var oldToken = list[0].token;
            if(token == oldToken){
                var time = new Date().getTime();
                if(time < list[0].time){
                    var sql1 = "SELECT c.id,c.shop_id,c.token,s.phone,s.shopname,s.shoptype,s.address from k_cashier as c LEFT JOIN k_shop as s ON s.id = c.shop_id WHERE token ='"+ token +"'";
                    console.log(sql1);
                    DB.query(sql1, function (er, list) {
                        if (!er&&list.length>0) {
                            msg.success = true;
                            msg.code = 100;
                            msg.data = list[0];
                            res.json(msg);
                        }
                    });
                }else{
                    msg.code = 102;
                    msg.msg = "登录失效";
                    res.json(msg);
                }
            }else{
                msg.code = 101;
                msg.msg = "用户token不匹配";
                res.json(msg);
            }
        }else {
            msg.code = 103;
            msg.msg="用户不存在";
            res.json(msg);
        }
    });
};

/**注册添加用户信息*/
exports.insertUserInfo=function(req,res,msg){
    var uid = IncreaseId.increaseId();
    var username = req.body.username;
    var sex = req.body.sex;
    var idcard = req.body.idcard;
    var phone = req.body.phone;
    var province =req.body.province;
    var city =req.body.city;
    var district =req.body.district;
    var address =req.body.address;
    if(uid){
        var sql = "INSERT INTO k_user(id,account,username,sex,idcard,phone) VALUES ('"
            +uid+"','"
            +phone+"','"
            +username+"','"
            +sex+"','"
            +idcard+"','"
            +phone+"');" ;
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                msg.success=true;
                msg.msg="用户信息注册成功";
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
        var id = IncreaseId.increaseId();
        var sql2 = "INSERT INTO k_address(id,uid,isdefault,realname,mobile,idnumber,province,city,district,address) VALUES ('"
            +id+"','"
            +uid+"',1,'"
            +username+"','"
            +phone+"','"
            +idcard+"','"
            +province+"','"
            +city+"','"
            +district+"','"
            +address+"')";
        console.log(sql2);
        DB.query(sql2,function(er,list){
            if(!er){
                msg.success=true;
                msg.msg="收货地址注册成功";
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
}


/**收银机商品信息*/
exports.getCashierProInfo = function(req,res,msg){
    var barcode = req.body.barcode;
    var shop_id = req.body.shop_id;
    if(barcode && shop_id) {
        var sql = "select g.barcode,g.goods_name,g.id,g.good_tax_rate,g.good_type,gs.price as shop_price from k_goods as g left join k_goods_shop gs on g.id=gs.goods_id where g.barcode='" + barcode + "' and gs.shop_id='" + shop_id + "'";
        console.log(sql);
        DB.query(sql, function (er, list) {
            if(!er&&list.length>0){
                msg.success=true;
                msg.data = list[0];
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
};


/**根据手机号查询用户信息*/
exports.getUserInfoByPhone = function(req,res,msg){
    var phone = req.body.phone;
    console.log(phone);
    if(phone) {
        var sql = "select u.id AS id,u.account AS account,u.username AS username,u.phone AS phone,u.idcard AS idcard,u.ispass AS ispass,u.creatdate AS creatdate,u.shopid AS shopid,u.photo AS photo,u.nickname AS nickname,u.login_time AS login_time,u.token AS token,u.jifen AS jifen,u.sex AS sex,a.realname,a.mobile,a.idnumber,a.zipcode,a.province,a.city,a.district,a.address FROM k_user as u LEFT JOIN k_address as a ON a.uid = u.id and a.isdefault = '1' where phone='" +phone+"'";
        console.log(sql);
        DB.query(sql, function (er, list) {
            if(!er&&list.length>0){
                msg.success=true;
                msg.data = list[0];
                res.json(msg);
            }else{
                msg.msg= "该手机号不存在";
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
};

/**根据身份证查询用户信息*/
exports.getUserInfoByIdcard = function(req,res,msg){
    var idcard = req.body.idcard;
    if(idcard) {
        var sql = "select u.id AS id,u.account AS account,u.username AS username,u.phone AS phone,u.idcard AS idcard,u.ispass AS ispass,u.creatdate AS creatdate,u.shopid AS shopid,u.photo AS photo,u.nickname AS nickname,u.login_time AS login_time,u.token AS token,u.jifen AS jifen,u.sex AS sex,a.realname,a.mobile,a.idnumber,a.zipcode,a.province,a.city,a.district,a.address FROM k_user as u LEFT JOIN k_address as a ON a.uid = u.id and a.isdefault = '1' where idcard='" +idcard+"'";
        console.log(sql);
        DB.query(sql, function (er, list) {
            if(!er&&list.length>0){
                msg.success=true;
                msg.data = list[0];
                res.json(msg);
            }else{
                msg.msg= "该身份证号不存在";
                res.json(msg);
            }
        });
    }else{
        msg.msg="参数有误";
        res.json(msg);
    }
};

/**生成订单*/
exports.insertOrderInfo = function(req,res,msg){
    var token = req.body.token;
    var goodsList = JSON.parse(req.body.goodsList);
    var orderId = IncreaseId.increaseId();
    var user_id = req.body.user_id;
    var shop_id = req.body.shop_id;
    var order_sn = req.body.order_sn;
    var goods_amount = req.body.goods_amount;
    var shipping_fee = req.body.shipping_fee;
    var order_amount = req.body.order_amount;
    var goods_tax = req.body.goods_tax;
    var add_time = req.body.add_time;
    var consignee = req.body.consignee;
    var province = req.body.province;
    var city = req.body.city;
    var district = req.body.district;
    var address = req.body.address;
    var zipcode = req.body.zipcode;
    var mobile = req.body.mobile;
    var postcard = req.body.postcard;
    var html = '';
    var sql = "SELECT token,time from k_cashier WHERE shop_id ='"+ shop_id +"'";
    console.log(sql);
    DB.query(sql, function (er, list) {
        if (!er&&list.length>0) {
            var oldToken = list[0].token;
            if(token == oldToken){
                if(user_id && goodsList.length>0) {
                    for (var i = 0; i < goodsList.length; i++) {
                        var id = IncreaseId.increaseId();
                        var a = "('" + id + "',"
                            + "'" + orderId + "',"
                            + "'" + goodsList[i].goods_id + "',"
                            + "'" + goodsList[i].goods_name + "',"
                            + "'" + goodsList[i].goods_price + "',"
                            + "'" + goodsList[i].goods_number + "',"
                            + "'" + goodsList[i].goods_type + "',"
                            + "'" + goodsList[i].goods_attr + "',"
                            + "'" + shop_id + "',"
                            + "'" + goodsList[i].good_tax_fee + "'),";
                        html += a;
                    }
                    html = html.substring(0, html.length - 1);
                    var sql1 = "INSERT into k_order_goods(id,order_id,goods_id,goods_name,goods_price,goods_number,good_type,goods_attr,shop_id,good_tax_fee) VALUES" + html;
                    var sql2 = "INSERT into k_order_info(id,order_sn,user_id,goods_amount,shipping_fee,order_amount,goods_tax,add_time,consignee,country,province,city,district,address,zipcode,mobile,postcard) VALUES ('"
                        + orderId + "',"
                        + "'" + order_sn + "',"
                        + "'" + user_id + "',"
                        + "'" + goods_amount + "',"
                        + "'" + shipping_fee + "',"
                        + "'" + order_amount + "',"
                        + "'" + goods_tax + "',"
                        + "'" + add_time + "',"
                        + "'" + consignee + "',"
                        + "'中国',"
                        + "'" + province + "',"
                        + "'" + city + "',"
                        + "'" + district + "',"
                        + "'" + address + "',"
                        + "'" + zipcode + "',"
                        + "'" + mobile + "',"
                        + "'" + postcard + "')";
                    console.log(sql1);
                    console.log(sql2);
                    DB.query(sql1, function (er, list) {
                        if (!er) {
                            DB.query(sql2, function (er, list) {
                                if (!er) {
                                    msg.success = true;
                                    msg.code = 100;
                                    msg.data = orderId;
                                    res.json(msg);
                                } else {
                                    msg.msg = er;
                                    res.json(msg);
                                }
                            });
                        } else {
                            msg.msg = er;
                            res.json(msg);
                        }
                    });
                }else{
                    msg.msg = er;
                    res.json(msg);
                }
            }else{
                msg.code = 101;
                msg.msg = "用户不匹配";
                res.json(msg);
            }
        }else {
            msg.code = 103;
            msg.msg= "用户不存在";
            res.json(msg);
        }
    });
};